using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Xml;

namespace SPDevExplorer
{
    public partial class DlgNewSite : Form
    {
        public DlgNewSite()
        {
            InitializeComponent();
        }

        public string SiteUrl
        {
            get { return idSiteUrl.Text; }
        }

        public string UserName
        {
            get { return idUserName.Text; }
        }

        public string Password
        {
            get { return idPassword.Text; }
        }

        public string Domain
        {
            get { return idDomain.Text; }
        }

        public bool Integrated
        {
            get { return idWindowsCredentials.Checked; }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            try
            {
                XmlElement oPrevious = ExplorerSettings.PreviousSites;
                if (oPrevious != null)
                {
                    for (int c = oPrevious.ChildNodes.Count - 1; c >= 0; c--)
                    {
                        if (oPrevious.ChildNodes[c] is XmlElement)
                        {
                            XmlElement oSite = (XmlElement) oPrevious.ChildNodes[c];
                            idSiteUrl.Items.Add(oSite.GetAttribute("Url"));
                        }
                    }

                    // Make the last selected site the default one
                    if (idSiteUrl.Items.Count > 0)
                    {
                        idSiteUrl.SelectedItem = idSiteUrl.Items[0];
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }

        private void idSpecificUser_CheckedChanged(object sender, EventArgs e)
        {
            lblSPPassword.Enabled = idSpecificUser.Checked;
            lblSPUserName.Enabled = idSpecificUser.Checked;
            idUserName.Enabled = idSpecificUser.Checked;
            idPassword.Enabled = idSpecificUser.Checked;
            idDomain.Enabled = idSpecificUser.Checked;
            lblDomain.Enabled = idSpecificUser.Checked;
        }

        private void idOK_Click(object sender, EventArgs e)
        {
            try
            {
                Trace.WriteLine(String.Format("Creating connection to {0}", idSiteUrl.Text));
                XmlElement oPrevious = ExplorerSettings.PreviousSites;

// SvdO: support all url scheme's, not only http://. People could use https://.
// Just don't test for missing http:// in front and add if missing, should be specified
#if false
                if (!idSiteUrl.Text.ToLower().StartsWith("http://")) idSiteUrl.Text = "http://" + idSiteUrl.Text;
#endif
// /SvdO

                if (oPrevious != null)
                {
                    XmlElement oNew = oPrevious.OwnerDocument.CreateElement("Site");
                    oNew.SetAttribute("Url", idSiteUrl.Text);
                    oNew.SetAttribute("UrlKey", idSiteUrl.Text.ToLower());

                    XmlNode oNode = oPrevious.SelectSingleNode("Site[@UrlKey='" + idSiteUrl.Text.ToLower() + "']");

                    if (oNode != null) oNode.ParentNode.RemoveChild(oNode);

                    oPrevious.AppendChild(oNew);
                    if (oPrevious.ChildNodes.Count > 10)
                        oPrevious.RemoveChild(oPrevious.ChildNodes[0]);

                    ExplorerSettings.Save();
                }
            }
            catch
            {
            }
        }
    }
}